<template>
    <div class="info-menu">
      <div class="info-logo" @click="$router.push(this.global.homeUrl)" :style="{'background-color':themeColor}" :class="collapse?'menu-bar-collapse-width':'menu-bar-width'">
        <img v-if="collapse" src="@/assets/logo.png"/> <div class="appName">{{collapse?'':appName}}</div>
      </div>
      <el-menu :default-active="'0'"
               ref="navmenu"
               class="navmenu"
               @select="handleSelect"
               :class="collapse?'menu-bar-collapse-width':'menu-bar-width'"
               :collapse="collapse" :collapse-transition="false" :unique-opened="true"
               active-text-color="#409eff">
        <!-- 导航菜单 -->
        <el-menu-item index="0" @click="()=>this.$router.push(this.global.homeUrl)">
          <i class="el-icon-date"></i>
          <span slot="title">仪表盘</span>
        </el-menu-item>
        <!-- 导航菜单树组件，动态加载菜单 -->
        <perm-tree v-for="menu in navTree" :key="menu.permId" :menu="menu"></perm-tree>
      </el-menu>
    </div>
</template>

<script>
import PermTree from '../../Component/PermTree/Index'
import { mapState } from 'vuex'
export default {
    components: { PermTree },
    name: 'NavBar',
    data() {
        return {
        }
    },
    computed: {
        ...mapState({
            appName: state => state.app.appName,
            navTree: state => state.menu.navTree,
            themeColor: state => state.app.themeColor,
            collapse: state => state.app.collapse
        }),
        mainTabs: {
            get() { return this.$store.state.tab.mainTabs },
            set(val) { this.$store.commit('updateMainTabs', val) }
        },
        activeTabName: {
            get() { return this.$store.state.tab.activeTabName },
            set(val) { this.$store.commit('updateActiveTabName', val) }
        }
    },
    watch: {
        $route: 'handleRoute'
    },
    created() {
        this.handleRoute(this.$router)
    },
    mounted() {
    // 监听异步路由加载完成后，自动打开当前路由
        const cd = setInterval(() => {
            if (this.$store.state.app.menuRouteLoaded) {
                cd && clearInterval(cd)
                this.handleRoute(this.$router.history.current)
            }
        }, 200)
    },
    methods: {
        handleSelect(key, keyPath) {
            // console.log(keyPath)
        },
        handleRoute(route) {
            if (!route.name) {
                return
            }
            var tab = this.mainTabs.filter(item => item.name === route.name)[0]
            if (!tab) {
                tab = {
                    name: route.name,
                    title: route.name,
                    icon: route.meta.icon
                }
                this.mainTabs = this.mainTabs.concat(tab)
            }
            this.activeTabName = tab.name
            // 切换标签时高亮同步菜单
            if (this.$refs.navmenu != null) {
                this.$refs.navmenu.activeIndex = '' + route.meta.index
                this.$refs.navmenu.initOpenedMenu()
            }
        }
    }
}
</script>

<style scoped>
  .info-menu{
    text-align: left;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 200px;
    padding: 0;
    z-index: 1020;
  }
  .info-logo{
    height: 60px;
    line-height: 60px;
    color: #fff;
    text-align: center;
  }
  .info-logo img{
    margin-top: 10px;
    padding: 0;
    width: 40px;
    height: 40px;
  }
  .info-logo .appName{
    font-weight: bold;
    font-size: 24px;
  }
  .navmenu{
    min-height: calc(100vh - 60px);
    cursor: pointer;
  }
  .menu-bar-width {
    width: 200px;
  }
  .menu-bar-collapse-width {
    width: 65px;
  }
</style>
